﻿/**
 * 
 * F. Mehmet ARSLAN (c) 2015
 * fmehmetarslan46@gmail.com
 *  
 **/
var app = {
    windowFocus: true,
    requestTime:5000,
    notificationSended : [],
    doNotNotification : [200,303,401],
    getStateList: function () {
        jQuery(".service-state loader").show();
        jQuery.ajax({
            url: "/home/StateService",
            dataType: "json",
            success: function (data) {
                if (data == "" || data == null || data == undefined)
                {
                    jQuery(".state-dashboard").append("<div class='alert error'>Sunucu ile bağlantı kurulamadı.</div>");
                } else {
                    jQuery(".state-dashboard > alert").remove();
                }
                if (data == "refresh") {
                    location.reload();
                } else {
                    for (var i = 0; i < data.length; i++) {
                        jQuery("#" + data[i].Id + "-state").removeClass();
                        jQuery("#" + data[i].Id + "-state").addClass("state state-" + data[i].StatusCode);
                        if (app.doNotNotification.indexOf(data[i].StatusCode)==-1)
                            app.notification(data[i]);
                        else {
                            var indexOf = app.notificationSended.indexOf(data[i].Id);
                            if (indexOf > -1) {
                                app.notificationSended.splice(indexOf, 1);
                            }
                        }
                    }
                }
            },
            error: function (xhr, status, error) {
                jQuery(".state-dashboard>.alert").remove();
                var msg = "";
                try {
                    var err = eval("(" + xhr.responseText + ")");
                    msg = err.Message;
                } catch (ex) {
                    msg = status;
                }
                
                jQuery(".state-dashboard > info").before("<div class='alert error'>Sunucu ile bağlantı kurulamadı. <br/> " + msg + "</div>");
            },
            complete: function () {
                setTimeout(function () { app.getStateList(); }, app.requestTime);
                jQuery(".service-state loader").hide();
            }
        });
    },
    notification: function (service) {
        if (app.windowFocus) {
            app.notificationSended.push(service.Id);
            return;
        }
        if (!Notification) {
            return;
        }
        if (app.notificationSended.indexOf(service.Id) > -1)
            return;
        if (Notification.permission !== "granted")
            Notification.requestPermission();

        var notification = new Notification('Sunucu Hatası [' + service.LongDescription + ']', {
            icon: 'http://appdev.company.com/resources/image/logo.png',
            body: service.LongDescription + " isimli makinada sorun Var"
        });

        notification.onclick = function () {
            window.focus(); this.cancel();
            //window.open("http://appdev.company.com");
        }
        app.notificationSended.push(service.Id);
    },
    widget:function(id){
        jQuery("#" + id + " loader").show();
        jQuery.ajax({
            url: "/widget/"+id,
            dataType: "html",
            success: function (data) {
                if (data == "" || data == null || data == undefined) {
                    jQuery("#" + id + " .widget-content").append("<div class='alert error'>Sunucu ile bağlantı kurulamadı.</div>");
                } else {
                    jQuery("#" + id + " .widget-content" + " .alert").remove();
                }
                if (data == "refresh") {
                    location.reload();
                } else {
                    jQuery("#" + id + " .widget-content").empty().append(data);
                }
            },
            error: function (xhr, status, error) {
                jQuery("#" + id + " .widget-content" + " .alert").remove();
                var msg = "";
                try {
                    var err = eval("(" + xhr.responseText + ")");
                    msg = err.Message;
                } catch (ex) {
                    msg = status;
                }

                jQuery("#" + id + " .widget-content").append("<div class='alert error'>Sunucu ile bağlantı kurulamadı. <br/> " + msg + "</div>");
            },
            complete: function () {
                //setTimeout(function () { app.widget(id); }, app.requestTime);
                jQuery("#" + id + " loader").hide();
            }
        });
    },
    log: function (line) {
        jQuery("#stateLogContainer>loader").show();
        jQuery.ajax({
            url: "/home/stateLog?line="+line,
            dataType: "html",
            success: function (data) {
                jQuery("#stateLog .alert").remove();
                if (data == "" || data == null || data == undefined) {
                    //jQuery("#stateLog").append("<div class='alert error'>Sunucu ile bağlantı kurulamadı.</div>");
                    return;
                }
                if (data == "refresh") {
                    location.reload();
                } else {
                    objDiv=jQuery("#stateLog");
                    objDiv.append(data);
                    objDiv[0].scrollTop = objDiv[0].scrollHeight;
                }
            },
            error: function (xhr, status, error) {
                jQuery("#stateLog .alert").remove();
                var msg = "";
                try {
                    var err = eval("(" + xhr.responseText + ")");
                    msg = err.Message;
                } catch (ex) {
                    msg = status;
                }

                jQuery("#stateLog").append("<div class='alert error'>Sunucu ile bağlantı kurulamadı. <br/> " + msg + "</div>");
            },
            complete: function (data) {
                var lines = data.responseText;
                var lineCount = lines.split("<br/>").length-1;                
                setTimeout(function () { app.log(line + lineCount); }, app.requestTime);
                jQuery("#stateLogContainer>loader").hide();
            }
        });
    },
    initialize: function () {
        jQuery(window).focus(function () {
            app.windowFocus = true;
        }).blur(function () {
            app.windowFocus = false;
        });
        app.getStateList();
        app.widget("jenkins");
        app.widget("appwiki");
        app.widget("sonar");
        app.log(0);
    }
}
jQuery(window).load(function () {
    app.initialize();
});
